//
//  YBBBSController.m
//  Cocoachina
//
//  Created by 尚往文化 on 17/9/14.
//  Copyright © 2017年 YBing. All rights reserved.
//

#import "YBBBSController.h"
#import "YBBBS.h"
#import <TFHpple.h>
#import <MJRefresh.h>
#import <UIImageView+WebCache.h>
#import "YBBBSCell.h"
#import "UITableViewCell+YB.h"
#import "YBNewsDetailController.h"

@interface YBBBSController ()

@property (nonatomic, strong) NSArray<NSArray<YBBBS *> *> *datas;
@property (nonatomic, strong) NSArray<NSString *> *titles;

@end

@implementation YBBBSController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSHTTPCookieStorage *sharedHTTPCookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    
    NSArray *cookies = [sharedHTTPCookieStorage cookiesForURL:[NSURL URLWithString:@"http://www.cocoachina.com/bbs/"]];
    
//    cookies = [sharedHTTPCookieStorage cookies];
    
    NSEnumerator *enumerator = [cookies objectEnumerator];
    
    NSHTTPCookie *cookie;
    
    while (cookie = [enumerator nextObject]) {
        
        NSLog(@"COOKIE{name: %@, value: %@}", [cookie name], [cookie value]);
        
    }
}

- (void)loadData
{
    [super loadData];
    NSString *url = @"http://www.cocoachina.com/bbs/";
    [YBHttpTool get:url success:^(TFHpple *doc) {
        NSMutableArray *datas = [self parseWithHpple:doc];
        self.datas = datas;
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

- (NSMutableArray<NSMutableArray<YBBBS *> *> *)parseWithHpple:(TFHpple *)doc
{
    NSArray<TFHppleElement *> *elements  = [doc searchWithXPathQuery:@"//div[@id='content']"];
    TFHppleElement *contentElement = [[elements firstObject] firstChildWithTagName:@"div"];
    
    NSArray<TFHppleElement *> *t_list = [contentElement childrenWithClassName:@"t"];
    NSMutableArray *titles = [NSMutableArray array];
    [t_list enumerateObjectsUsingBlock:^(TFHppleElement * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        TFHppleElement *table = [obj firstChildWithTagName:@"table"];
        [titles addObject:[[[[[[table firstChildWithTagName:@"tr"] firstChildWithTagName:@"th"] firstChildWithTagName:@"div"] firstChildWithTagName:@"div"] firstChildWithTagName:@"h2"] firstChildWithTagName:@"a"].text];
    }];
    self.titles = titles;
    
    NSArray<TFHppleElement *> *forum_center = [contentElement childrenWithClassName:@"forum_center"];
    
    NSMutableArray<NSMutableArray<YBBBS *> *> *bbs = [NSMutableArray array];
    
    [forum_center enumerateObjectsUsingBlock:^(TFHppleElement * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSMutableArray *arr = [NSMutableArray array];
        TFHppleElement *div = [[[[obj firstChildWithTagName:@"div"] firstChildWithTagName:@"div"] firstChildWithTagName:@"div"] childrenWithTagName:@"div"][1];
        TFHppleElement *table = [div firstChildWithTagName:@"table"];
        [[[table firstChildWithTagName:@"tbody"] childrenWithClassName:@"tr3"] enumerateObjectsUsingBlock:^(TFHppleElement *obj1, NSUInteger idx, BOOL * _Nonnull stop) {
            YBBBS *bbs = [YBBBS new];
            TFHppleElement *a = [[[obj1 firstChildWithTagName:@"th"] firstChildWithTagName:@"h3"] firstChildWithTagName:@"a"];
            bbs.url = [a objectForKey:@"href"];
            TFHppleElement *font = [a firstChildWithTagName:@"font"];
            if (font) {
                bbs.title = font.text;
            } else {
                bbs.title = a.text;
            }
            bbs.desc = [[obj1 firstChildWithTagName:@"th"] firstChildWithClassName:@"smalltxt gray"].text;
            bbs.img = [[[[[obj1 childrenWithTagName:@"td"][1] firstChildWithTagName:@"span"] firstChildWithTagName:@"a"] firstChildWithTagName:@"img"] objectForKey:@"src"];
            if (bbs.img.length) {
                [arr addObject:bbs];
            }
        }];
        if (arr.count) {
            [bbs addObject:arr];
        }
    }];
    
    return bbs;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas[section].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YBBBSCell *cell = [YBBBSCell cellWithTableView:tableView];
    YBBBS *bbs = self.datas[indexPath.section][indexPath.row];
    cell.bbs = bbs;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YBBBS *bbs = self.datas[indexPath.section][indexPath.row];
    YBNewsDetailController *vc = [YBNewsDetailController new];
    vc.url = [NSString stringWithFormat:@"http://www.cocoachina.com/bbs/%@", bbs.url];
    vc.title = bbs.title;
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 34;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.titles[section];
}



@end
